using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel.Design;
using System.Drawing.Design;
using System.Drawing;
using System.ComponentModel;
using System.Drawing.Drawing2D;
using System.IO;
using System.Reflection;
using System.Diagnostics;

namespace XnaDesignerGenerator
{
    public class RootDesignerView2 : UserControl
    {
        // Fields
        private bool changingSelection = false;
        private IContainer components = null;
        private ListBox comps;
        private GameManagerDesigner designer;
        private FlowLayoutPanel flowLayoutPanel1;
        private Label label1;
        private SplitContainer mainView;
        private PictureBox pictureBox1;
        private PictureBox pictureBox2;
        private SplitContainer splitContainer1;
        private ListBox svcs;

        // Methods
        public RootDesignerView2(GameManagerDesigner designer)
        {
            this.designer = designer;
            this.InitializeComponent();
            this.label1.Text = designer.Component.Site.Name;
            designer.GetService<ISelectionService>().SelectionChanged += new EventHandler(this.RootDesignerView2_SelectionChanged);
            this.DataBind();
            if (designer.Component.Game != null)
            {
                Type type = designer.Component.Game.GetType();
                string[] manifestResourceNames = type.Assembly.GetManifestResourceNames();
                foreach (string str in manifestResourceNames)
                {
                    Stream stream = null;
                    try
                    {
                        using (stream = type.Assembly.GetManifestResourceStream(str))
                        {
                            
                            if (this.pictureBox1.Image == null)
                            {
                                this.pictureBox1.Image = new Icon(stream).ToBitmap();
                            }
                            else
                                break;
                        }
                    }
                    catch (Exception)
                    {
                        Debug.WriteLine(str + " is not an Icon");
                        if ((stream != null) && stream.CanRead)
                        {
                            stream.Seek(0L, SeekOrigin.Begin);
                        }
                    }
                }
            }
        }

        public void DataBind()
        {
            this.svcs.Items.Clear();
            this.comps.Items.Clear();
            foreach (IComponent component in this.designer.Component.Container.Components)
            {
                if (component != this.designer.Component)
                {
                    if (component is XnaGameService)
                    {
                        this.svcs.Items.Add(component);
                    }
                    if (component is XnaGameComponent)
                    {
                        this.comps.Items.Add(component);
                    }
                    if (component is XnaGame)
                    {
                        this.label1.Tag = component;
                    }
                }
            }
            this.Refresh();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void DragDropComponent(object sender, DragEventArgs e)
        {
            IToolboxService toolboxService = this.ToolboxService;
            if ((toolboxService != null) && (toolboxService.GetSelectedToolboxItem(this.DesignerHost) != null))
            {
                ToolboxItem selectedToolboxItem = toolboxService.GetSelectedToolboxItem(this.DesignerHost);
                this.designer.GetService<ISelectionService>().SetSelectedComponents(selectedToolboxItem.CreateComponents(this.DesignerHost));
                this.DataBind();
            }
        }

        private void DragEnterComponent(object sender, DragEventArgs e)
        {
            ToolboxItem selectedToolboxItem = this.ToolboxService.GetSelectedToolboxItem();
            Type type = Type.GetType(selectedToolboxItem.TypeName, false);
            if (!((type != null) || string.IsNullOrEmpty(selectedToolboxItem.AssemblyName.FullName)))
            {
                type = Assembly.Load(selectedToolboxItem.AssemblyName).GetType(selectedToolboxItem.TypeName, false);
            }
            if (this.EqualsWithOldVersion(type, typeof(XnaGameComponent)) || type.IsAssignableFrom(typeof(XnaGameComponent)))
            {
                e.Effect = DragDropEffects.Move | DragDropEffects.Copy | DragDropEffects.Scroll;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }

        private void DragEnterGame(object sender, DragEventArgs e)
        {
            ToolboxItem selectedToolboxItem = this.ToolboxService.GetSelectedToolboxItem();
            Type type = Type.GetType(selectedToolboxItem.TypeName, false);
            if (!((type != null) || string.IsNullOrEmpty(selectedToolboxItem.AssemblyName.FullName)))
            {
                type = Assembly.Load(selectedToolboxItem.AssemblyName).GetType(selectedToolboxItem.TypeName, false);
            }
            if (this.EqualsWithOldVersion(type, typeof(XnaGame)) || type.IsAssignableFrom(typeof(XnaGame)))
            {
                e.Effect = DragDropEffects.Move | DragDropEffects.Copy | DragDropEffects.Scroll;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }

        private void DragEnterService(object sender, DragEventArgs e)
        {
            ToolboxItem selectedToolboxItem = this.ToolboxService.GetSelectedToolboxItem();
            Type type = Type.GetType(selectedToolboxItem.TypeName, false);
            if (!((type != null) || string.IsNullOrEmpty(selectedToolboxItem.AssemblyName.FullName)))
            {
                type = Assembly.Load(selectedToolboxItem.AssemblyName).GetType(selectedToolboxItem.TypeName, false);
            }
            if (this.EqualsWithOldVersion(type, typeof(XnaGameService)) || type.IsAssignableFrom(typeof(XnaGameService)))
            {
                e.Effect = DragDropEffects.Move | DragDropEffects.Copy | DragDropEffects.Scroll;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }

        private bool Equals(byte[] array1, byte[] array2)
        {
            if (array1.Length != array2.Length)
            {
                return false;
            }
            for (int i = 0; i < array1.Length; i++)
            {
                if (array1[i] != array2[i])
                {
                    return false;
                }
            }
            return true;
        }

        private bool EqualsWithOldVersion(Type type, Type type_2)
        {
            return ((type == type_2) || ((this.Equals(type.Assembly.GetName().GetPublicKey(), type_2.Assembly.GetName().GetPublicKey()) && (type.Assembly.GetName().Name == type_2.Assembly.GetName().Name)) && (type.FullName == type_2.FullName)));
        }

        private T GetService<T>()
        {
            T service = this.designer.GetService<T>();
            if (service != null)
            {
                return service;
            }
            return (T)this.GetService(typeof(T));
        }

        private void InitializeComponent()
        {
            this.comps = new ListBox();
            this.label1 = new Label();
            this.svcs = new ListBox();
            this.splitContainer1 = new SplitContainer();
            this.pictureBox1 = new PictureBox();
            this.flowLayoutPanel1 = new FlowLayoutPanel();
            this.mainView = new SplitContainer();
            this.pictureBox2 = new PictureBox();
            this.splitContainer1.Panel1.SuspendLayout();
            this.splitContainer1.Panel2.SuspendLayout();
            this.splitContainer1.SuspendLayout();
            ((ISupportInitialize)this.pictureBox1).BeginInit();
            this.flowLayoutPanel1.SuspendLayout();
            this.mainView.Panel1.SuspendLayout();
            this.mainView.Panel2.SuspendLayout();
            this.mainView.SuspendLayout();
            ((ISupportInitialize)this.pictureBox2).BeginInit();
            base.SuspendLayout();
            this.comps.AllowDrop = true;
            this.comps.BackColor = Color.LightBlue;
            this.comps.Dock = DockStyle.Fill;
            this.comps.FormattingEnabled = true;
            this.comps.Location = new Point(0, 20);
            this.comps.Name = "comps";
            this.comps.Size = new Size(167, 173);
            this.comps.TabIndex = 0;
            this.comps.SelectedIndexChanged += new EventHandler(this.SelectComponent);
            this.comps.DragDrop += new DragEventHandler(this.DragDropComponent);
            this.comps.DragEnter += new DragEventHandler(this.DragEnterComponent);
            this.comps.KeyDown += new KeyEventHandler(this.KeyDesignerDown);
            this.label1.AllowDrop = true;
            this.label1.AutoSize = true;
            this.label1.Font = new Font("Microsoft Sans Serif", 15.75f, FontStyle.Regular, GraphicsUnit.Point, 0);
            this.label1.Location = new Point(41, 14);
            this.label1.Margin = new Padding(3, 10, 3, 0);
            this.label1.Name = "label1";
            this.label1.Size = new Size(70, 25);
            this.label1.TabIndex = 1;
            this.label1.Text = "label1";
            this.label1.Click += new EventHandler(this.SelectGame);
            this.svcs.AllowDrop = true;
            this.svcs.BackColor = Color.LightBlue;
            this.svcs.Dock = DockStyle.Fill;
            this.svcs.FormattingEnabled = true;
            this.svcs.Location = new Point(20, 20);
            this.svcs.Name = "svcs";
            this.svcs.Size = new Size(73, 173);
            this.svcs.TabIndex = 2;
            this.svcs.SelectedIndexChanged += new EventHandler(this.SelectService);
            this.svcs.DragDrop += new DragEventHandler(this.DragDropComponent);
            this.svcs.DragEnter += new DragEventHandler(this.DragEnterService);
            this.svcs.KeyDown += new KeyEventHandler(this.KeyDesignerDown);
            this.splitContainer1.AllowDrop = true;
            this.splitContainer1.Dock = DockStyle.Fill;
            this.splitContainer1.Location = new Point(0, 0);
            this.splitContainer1.Name = "splitContainer1";
            this.splitContainer1.Panel1.AllowDrop = true;
            this.splitContainer1.Panel1.Controls.Add(this.svcs);
            this.splitContainer1.Panel1.Padding = new Padding(20, 20, 0, 10);
            this.splitContainer1.Panel2.AllowDrop = true;
            this.splitContainer1.Panel2.Controls.Add(this.comps);
            this.splitContainer1.Panel2.Padding = new Padding(0, 20, 20, 10);
            this.splitContainer1.Size = new Size(284, 210);
            this.splitContainer1.SplitterDistance = 93;
            this.splitContainer1.TabIndex = 3;
            this.pictureBox1.Location = new Point(3, 7);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new Size(32, 32);
            this.pictureBox1.TabIndex = 4;
            this.pictureBox1.TabStop = false;
            this.flowLayoutPanel1.AllowDrop = true;
            this.flowLayoutPanel1.Controls.Add(this.pictureBox1);
            this.flowLayoutPanel1.Controls.Add(this.label1);
            this.flowLayoutPanel1.Dock = DockStyle.Fill;
            this.flowLayoutPanel1.Location = new Point(0, 0);
            this.flowLayoutPanel1.Name = "flowLayoutPanel1";
            this.flowLayoutPanel1.Padding = new Padding(0, 4, 0, 0);
            this.flowLayoutPanel1.Size = new Size(284, 50);
            this.flowLayoutPanel1.TabIndex = 5;
            this.flowLayoutPanel1.Click += new EventHandler(this.SelectGame);
            this.flowLayoutPanel1.DragDrop += new DragEventHandler(this.DragDropComponent);
            this.flowLayoutPanel1.DragEnter += new DragEventHandler(this.DragEnterGame);
            this.mainView.AllowDrop = true;
            this.mainView.Dock = DockStyle.Fill;
            this.mainView.FixedPanel = FixedPanel.Panel1;
            this.mainView.IsSplitterFixed = true;
            this.mainView.Location = new Point(0, 0);
            this.mainView.Name = "mainView";
            this.mainView.Orientation = Orientation.Horizontal;
            this.mainView.Panel1.AllowDrop = true;
            this.mainView.Panel1.BackColor = Color.LightBlue;
            this.mainView.Panel1.Controls.Add(this.pictureBox2);
            this.mainView.Panel1.Controls.Add(this.flowLayoutPanel1);
            this.mainView.Panel2.AllowDrop = true;
            this.mainView.Panel2.Controls.Add(this.splitContainer1);
            this.mainView.Panel2.DragDrop += new DragEventHandler(this.DragDropComponent);
            this.mainView.Panel2.DragEnter += new DragEventHandler(this.DragEnterGame);
            this.mainView.Size = new Size(284, 264);
            this.mainView.TabIndex = 6;
            this.mainView.DragEnter += new DragEventHandler(this.DragEnterGame);
            this.pictureBox2.Dock = DockStyle.Right;
            this.pictureBox2.Image = XnaDesignerGenerator.Properties.Resources.logo;
            this.pictureBox2.Location = new Point(165, 0);
            this.pictureBox2.Name = "pictureBox2";
            this.pictureBox2.Size = new Size(119, 50);
            this.pictureBox2.TabIndex = 6;
            this.pictureBox2.TabStop = false;
            this.AllowDrop = true;
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            this.BackColor = Color.PowderBlue;
            base.Controls.Add(this.mainView);
            base.Name = "RootDesignerView2";
            base.Size = new Size(284, 264);
            base.Click += new EventHandler(this.SelectGame);
            base.DragEnter += new DragEventHandler(this.DragEnterGame);
            this.splitContainer1.Panel1.ResumeLayout(false);
            this.splitContainer1.Panel2.ResumeLayout(false);
            this.splitContainer1.ResumeLayout(false);
            ((ISupportInitialize)this.pictureBox1).EndInit();
            this.flowLayoutPanel1.ResumeLayout(false);
            this.flowLayoutPanel1.PerformLayout();
            this.mainView.Panel1.ResumeLayout(false);
            this.mainView.Panel2.ResumeLayout(false);
            this.mainView.ResumeLayout(false);
            ((ISupportInitialize)this.pictureBox2).EndInit();
            base.ResumeLayout(false);
        }

        private void KeyDesignerDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                this.DataBind();
            }
        }

        private void RootDesignerView2_SelectionChanged(object sender, EventArgs e)
        {
            if (!this.changingSelection)
            {
                this.DataBind();
            }
        }

        private void SelectComponent(object sender, EventArgs e)
        {
            if (!this.changingSelection)
            {
                this.changingSelection = true;
                this.svcs.SelectedIndex = -1;
                if (this.comps.SelectedIndex > -1)
                {
                    this.designer.GetService<ISelectionService>().SetSelectedComponents(new object[] { this.comps.SelectedItem });
                }
                this.changingSelection = false;
            }
        }

        private void SelectGame(object sender, EventArgs e)
        {
            if (!(this.changingSelection || (this.label1.Tag == null)))
            {
                this.changingSelection = true;
                this.svcs.SelectedIndex = -1;
                this.comps.SelectedIndex = -1;
                this.designer.GetService<ISelectionService>().SetSelectedComponents(new object[] { this.label1.Tag });
                this.changingSelection = false;
            }
        }

        private void SelectService(object sender, EventArgs e)
        {
            if (!this.changingSelection)
            {
                this.changingSelection = true;
                this.comps.SelectedIndex = -1;
                if (this.svcs.SelectedIndex > -1)
                {
                    this.designer.GetService<ISelectionService>().SetSelectedComponents(new object[] { this.svcs.SelectedItem });
                }
                this.changingSelection = false;
            }
        }

        // Properties
        public IDesignerHost DesignerHost
        {
            get
            {
                return this.GetService<IDesignerHost>();
            }
        }

        public IToolboxService ToolboxService
        {
            get
            {
                return this.GetService<IToolboxService>();
            }
        }
    }
}
